package com.xizi.秋招笔试.bilibili;

import java.util.Scanner;

/**
 * @author xizizzz
 * @description: TODO
 * @date 2021-8-25下午 07:25
 */
public class B {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[][] ans = new int[300][300];
        int index = 0;
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            String[] split = s.split(",");
            for (int i = 0; i < split.length; i++) {
                ans[index][i] = Integer.parseInt(split[i]);
            }
            index++;
        }
        final int res = numIslands(ans);
        System.out.println(res);
    }


    public static int numIslands(int[][] grid) {
        if (grid == null || grid.length == 0) return 0;
        int count = 0;
        //搜索每一个为1的点
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                //当前格子是1
                if (grid[i][j] == 1) {
                    count++;
                    //将改区域的1全部修改为0
                    dfs(grid, i, j);
                }
            }
        }
        return count;
    }

    //dfs搜索1并修改为0
    public static void dfs(int[][] grid, int i, int j) {
        if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0) {
            return;
        }
        grid[i][j] = 0;
        dfs(grid, i - 1, j);
        dfs(grid, i + 1, j);
        dfs(grid, i, j - 1);
        dfs(grid, i, j + 1);
    }
}
